//
//  RegisterViewController.m
//  Taste
//
//  Created by 徐昊 on 16/8/29.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "RegisterViewController.h"
#import "AFNetworking.h"
#import "SetPassWordViewController.h"
#import "NSString+Vali.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *code;
@property (weak, nonatomic) IBOutlet UIButton *regist;
@property (nonatomic, strong) NSString *codeStr;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"注册";
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"LZUniversal_BarButtonItem_Pop"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    backBtn.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = backBtn;
    [self setUp];
}
- (void)back:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setUp {
    _regist.layer.borderColor = [[UIColor blackColor] CGColor];
    _regist.layer.borderWidth = 1.0f;
    _regist.layer.cornerRadius = 10.0f;
    _regist.layer.masksToBounds = YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_phoneNumber resignFirstResponder];
    [_code resignFirstResponder];
}

- (IBAction)getCode:(id)sender {
    if ([NSString valiMobile:_phoneNumber.text]) {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *postURLString = @"http://app.legendzest.cn/index3.php?m=api250&c=register&a=getcode";
        NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
        [set addObject:@"text/html"];
        [manager.responseSerializer setAcceptableContentTypes:set];
        [manager.requestSerializer setValue:@"2.50" forHTTPHeaderField:@"version"];
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:@"23" forHTTPHeaderField:@"Content-Length"];
        NSString *str = [NSString stringWithFormat:@"phone=%@&type=", _phoneNumber.text];
        NSString *parameters = str;
        // 3. 设置请求参数的字符串格式, 自定义
        [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
            return parameters;
        }];
        // 正常POST发送
        [manager POST:postURLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"请求成功: %@", responseObject);
            _codeStr = [[responseObject objectForKey:@"res"] objectForKey:@"m_code"];
            NSLog(@"%@", _codeStr);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败: %@", error);
        }];
    } else {
        NSLog(@"请求失败");
    }
}
- (IBAction)register:(id)sender {
    if (_code.text == _codeStr) {
        SetPassWordViewController *vc = [[SetPassWordViewController alloc] init];
        vc.phoneNumber = _phoneNumber.text;
        vc.code = _codeStr;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        NSLog(@"验证码输入错误");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
